<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .carousel {
            width: 650px;
            height: 360px;
            margin: 0 auto;
            position: relative;
            overflow: hidden;
        }
        
        .carousel ul {
            list-style: none;
            width: 6000px;
            position: relative;
            left: -650px;
            transition: left 0.5s linear 0s;
        }
        
        .carousel ul li {
            float: left;
        }
        
        .carousel .leftbtn {
            display: block;
            position: absolute;
            left: 20px;
            width: 50px;
            height: 50px;
            top: 50%;
            margin-top: -25px;
            background-color: orange;
            border-radius: 50%;
        }
        
        .carousel .rightbtn {
            display: block;
            position: absolute;
            right: 20px;
            width: 50px;
            height: 50px;
            top: 50%;
            margin-top: -25px;
            background-color: orange;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="carousel">
        <ul id="list">
            <li>
                <img src="D:\BaiduNetdiskDownload\前端体系\images\0.jpg" alt="" />
            </li>
            <li>
                <img src="D:\BaiduNetdiskDownload\前端体系\images\1.jpg" alt="" />
            </li>
            <li>
                <img src="D:\BaiduNetdiskDownload\前端体系\images\2.jpg" alt="" />
            </li>
            <li>
                <img src="D:\BaiduNetdiskDownload\前端体系\images\3.jpg" alt="" />
            </li>
            <li>
                <img src="D:\BaiduNetdiskDownload\前端体系\images\4.jpg" alt="" />
            </li>
        </ul>
        <a href="javascript:;" id="leftbtn" class="leftbtn"></a>
        <a href="javascript:;" id="rightbtn" class="rightbtn"></a>
    </div>
    <script>
        // 得到按钮
        var leftbtn = document.getElementById("leftbtn");
        var rightbtn = document.getElementById("rightbtn");
        var ul = document.getElementById("list");

        // 当前ul显示到第几张
        var idx = 0;
        var lock = true;
        // 监听
        rightbtn.onclick = function() {
            if (!lock) return;
            lock = false;
            // 最后一张图片会把过度去掉，要重新加上
            list.style.transition = "left .5s linear 0s;";
            idx++;
            if (idx > 4) {
                //设置一个延时器，延时器的功能就是将ul瞬间拉回0的位置,延时器的目的就是让过度动画结束之后
                setTimeout(function() {
                    // 取消掉过渡，因为要的是瞬间移动，不是咕噜 回去
                    list.style.transition = "none";
                    list.style.left = 0;
                    idx = 0;
                }, 20);
            }
            ul.style.left = -idx * 650 + "px";
            // 函数节流
            setTimeout(function() {
                lock = true;
            }, 500);
        };
        // 左面按钮
        leftbtn.onclick = function() {
            if (!lock) return;
            lock = false;
            if (idx == 0) {
                // 取消掉过渡，因为要的是瞬间移动，不是咕噜 回去
                list.style.transition = "none";
                list.style.left = -5 * 650 + "px";
                idx = 4;
                // 设置一个延时器，这个延时器的延时可以是0毫秒，虽然是0毫秒，但是就可以让我们过度先是瞬间取消，然后再加上
                setTimeout(function() {
                    list.style.transition = "left .5s linear 0s;";
                    idx = 4;
                    ul.style.left = -idx * 650 + "px";
                }, 0);
            } else {
                idx--;
                ul.style.left = -idx * 650 + "px";
            }
            // 函数节流
            setTimeout(function() {
                lock = true;
            }, 300);
        };
    </script>
</body>

</html>